package com.ulinkle.ulinkle_muhf;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import androidx.activity.EdgeToEdge;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.speedata.libuhf.IUHFService;
import com.speedata.libuhf.UHFManager;
import com.speedata.libuhf.bean.SpdInventoryData;
import com.speedata.libutils.CommonUtils;
import com.ulinkle.ulinkle_muhf.base.BaseActivity;
import com.ulinkle.ulinkle_muhf.libutils.ErrorStatus;
import com.ulinkle.ulinkle_muhf.libutils.SharedXmlUtil;
import com.ulinkle.ulinkle_muhf.libutils.ToastUtil;
import com.ulinkle.ulinkle_muhf.libutils.excel.EPCBean;
import com.ulinkle.ulinkle_muhf.model.UhfCardAdapter;
import com.ulinkle.ulinkle_muhf.model.UhfCardBean;
import com.ulinkle.ulinkle_muhf.presenter.MainPresenter;
import com.ulinkle.ulinkle_muhf.uhf.UHFUtil;
import com.ulinkle.ulinkle_muhf.view.IMain;
import com.ulinkle.ulinkle_muhf.weight.dialog.DefaultSettingDialog;
import com.ulinkle.ulinkle_muhf.weight.floatball.FloatWarnManager;
import com.ulinkle.ulinkle_muhf.weight.floatwindow.FloatWindow;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import jxl.format.Colour;

public class MainActivity extends BaseActivity implements IMain, View.OnClickListener {
    private MainPresenter presenter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        presenter = new MainPresenter(this);
    }


    private TextView mEtSearch;
    private AlertDialog alertDialog1;
    private Button mFindBtn;
    private LinearLayout mLlFind, mLlListBg, mLlPause;
    private TextView mTvListMsg;
    private ToggleButton mTbtnSound;
    private TextView tagNumTv;
    private TextView speedTv;
    private TextView totalTime;
    private TextView mVersionTv;
    private TextView mSerialGPIOInfor;
    private Button btnExport;
    private Button mBtSearch;
    private ImageView mIvSet;
    private ImageView mIvMenu;
    private Button btnInsert;
    private EditText edvInsert;
    public void initView() {
        btnInsert = findViewById(R.id.btnInsert);
        btnInsert.setOnClickListener(this);
        edvInsert = findViewById(R.id.edvInsertF);
        mIvMenu = findViewById(R.id.iv_menu);
        mIvMenu.setOnClickListener(this);
        mIvSet = (ImageView) findViewById(R.id.iv_set);
        mBtSearch = (Button) findViewById(R.id.bt_search);
        mEtSearch = findViewById(R.id.et_search);
        mEtSearch.setOnClickListener(this);
        mIvSet.setOnClickListener(this);
        mBtSearch.setOnClickListener(this);
        mFindBtn = (Button) findViewById(R.id.btn_find);
        mFindBtn.setOnClickListener(this);
        mLlFind = (LinearLayout) findViewById(R.id.ll_find_layout);
        mLlPause = (LinearLayout) findViewById(R.id.ll_pause_layout);
        mTvListMsg = (TextView) findViewById(R.id.tv_list_msg);
        mTbtnSound = (ToggleButton) findViewById(R.id.t_btn_sound);
        tagNumTv = (TextView) findViewById(R.id.tv_number);
        speedTv = (TextView) findViewById(R.id.speed_tv);
        totalTime = (TextView) findViewById(R.id.totalTime);
        btnExport = (Button) findViewById(R.id.btn_export);
        btnExport.setOnClickListener(this);

        mLlListBg = (LinearLayout) findViewById(R.id.ll_list_bg);
        mListViewCard = (ListView) findViewById(R.id.lv_card);

        mLlFind.setVisibility(View.VISIBLE);
        mLlPause.setVisibility(View.GONE);
        mTvListMsg.setVisibility(View.GONE);
        mLlListBg.setVisibility(View.VISIBLE);
        mListViewCard.setVisibility(View.GONE);
        mVersionTv = (TextView) findViewById(R.id.tv_version_model);
        mSerialGPIOInfor = (TextView) findViewById(R.id.tv_serialport_gpio);
        mVersionTv.setText(CommonUtils.getAppVersionName(this));

    }



    private ListView mListViewCard;
     private UhfCardAdapter uhfCardAdapter;
     private List<UhfCardBean> uhfCardBeanList = new ArrayList<>();
     private boolean inSearch = false;
     private static final String CHARGING_PATH = "/sys/class/misc/bq25601/regdump/";
     public void initData() {
        model = UHFManager.getUHFModel();
        // 加载适配器 Load adapter
        uhfCardAdapter = new UhfCardAdapter(this, R.layout.item_uhf_card, uhfCardBeanList);
        mListViewCard.setAdapter(uhfCardAdapter);
        // 列表回调item点击事件 List callback item click event
        mListViewCard.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (inSearch) {
                    return;
                }

                String epcStr = uhfCardBeanList.get(position).getEpc();
                boolean u8 = SharedXmlUtil.getInstance(MainActivity.this).read("U8", false);
                if (u8) {
                    epcStr = epcStr.substring(0, 24);
                }
                int res = iuhfService.selectCard(1, epcStr, true);
                if (res == 0) {
                    // 选卡成功 Selected card success
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putString("epcName", epcStr);
                    bundle.putString("model", model);
                    intent.putExtras(bundle);
                    intent.setClass(MainActivity.this, CurrentCardActivity.class);
                    startActivity(intent);

                }
            }
        });
        UHFUtil.isOpenServer = false;
        file = new File(CHARGING_PATH);
        mVersionTv.append("-" + model);
        mSerialGPIOInfor.setText(SharedXmlUtil.getInstance(this).read("port", "")
                + "|" + SharedXmlUtil.getInstance(this).read("power_type", "")
                + "|" + SharedXmlUtil.getInstance(this).read("gpios", ""));
    }

    @Override
    public void floatWarn() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                FloatWarnManager.getInstance(getApplicationContext(), getResources().getString(R.string.dialog_uhf_off));
                FloatWarnManager floatWarnManager = FloatWarnManager.getFloatWarnManager();
                if (floatWarnManager != null) {
                    FloatWindow.get("FloatWarnTag").show();
                }
            }
        });
    }

    @Override
    public void openFail() {
        Toast.makeText(this, "Open serialport failed", Toast.LENGTH_SHORT).show();
        new AlertDialog.Builder(this).setTitle(R.string.DIA_ALERT).setMessage(R.string.DEV_OPEN_ERR).setPositiveButton(R.string.DIA_CHECK, (dialog, which) -> finish()).show();
    }


    private String model;
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.iv_set) {
            if (UHFUtil.isFastMode && model.contains(UHFManager.FACTORY_XINLIAN)) {
                //开启快速模式禁用设置  Disable Settings if fast mode is enabled
                ToastUtil.customToastView(this, getResources().getString(R.string.toast_stop_fast_tips), Toast.LENGTH_SHORT
                        , (TextView) LayoutInflater.from(this).inflate(R.layout.layout_toast, null));
            } else {
                //设置  Enter the Settings
                Intent intent = new Intent(this, InvSetActivity.class);
                startActivity(intent);
            }
        } else if (view.getId() == R.id.bt_search) {
            //搜索 search
            String epc = mEtSearch.getText().toString();
            Log.d("zzc", "epc:" + epc);
            if (epc.isEmpty()) {
                Toast.makeText(this, getResources().getString(R.string.search_toast), Toast.LENGTH_SHORT).show();
                return;
            }
            Intent intent1 = new Intent(this, SearchDirectionActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("epcNumber", mEtSearch.getText().toString());
            intent1.putExtras(bundle);
            startActivity(intent1);
        } else if (view.getId() == R.id.btn_find) {
//寻卡  inventory
            if (inSearch) {
                stopUhf();
            } else {
                startUhf();
            }
        } else if (view.getId() == R.id.btn_export) {
            //导出  export
            kProgressHUD = KProgressHUD.create(this)
                    .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                    .setCancellable(false)
                    .setAnimationSpeed(2)
                    .setDimAmount(0.5f)
                    .show();
            if (uhfCardBeanList.size() > 0) {
                ExportThread exportThread = new ExportThread();
                exportThread.start();
            } else {
                kProgressHUD.dismiss();
                boolean cn = "CN".equals(this.getApplicationContext().getResources().getConfiguration().locale.getCountry());
                if (cn) {
                    Toast.makeText(this, "没有数据，请先盘点", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "No data, please take stock", Toast.LENGTH_SHORT).show();
                }
            }
        } else if (view.getId() == R.id.et_search) {
            String[] items = {getResources().getString(R.string.search_dialog_item)};
            Log.d("zzc", "changdu " + uhfCardBeanList.size());
            if (uhfCardBeanList.size() != 0) {
                items = new String[uhfCardBeanList.size()];
                for (int i = 0; i < uhfCardBeanList.size(); i++) {
                    items[i] = uhfCardBeanList.get(i).getEpc();
                }
            }
            AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
            alertBuilder.setTitle(getResources().getString(R.string.search_dialog_title));
            alertBuilder.setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    if (uhfCardBeanList.size() != 0) {
                        mEtSearch.setText(uhfCardBeanList.get(i).getEpc());
                    }
                    alertDialog1.dismiss();
                }
            });
            alertDialog1 = alertBuilder.create();
            alertDialog1.show();
        } else if (view.getId() == R.id.iv_menu) {
            DefaultSettingDialog defaultSettingDialog = new DefaultSettingDialog(this, iuhfService);
            defaultSettingDialog.show();
        } else if (view.getId() == R.id.btnInsert) {
            String s = edvInsert.getText().toString();
            int frequency = 0;
            if (!s.isEmpty() || !s.equals("")) {
                frequency = Integer.parseInt(s);
            }
            byte[] bytes = iuhfService.insertFrequency(frequency);
            if (bytes != null) {
                new AlertDialog.Builder(MainActivity.this).
                        setMessage(DataConversionUtils.byteArrayToString(bytes)).setPositiveButton("sure", null).show();
            }
        }
    }
    private IUHFService iuhfService;
    private File file;
    private BufferedWriter writer;
    /**
     * 开始盘点
     * Start inventory
     */
    private void startUhf() {
        if (iuhfService == null) {
            return;
        }
        uhfCardBeanList.clear();
        uhfCardAdapter.notifyDataSetChanged();
        scant = 0;
        startCheckingTime = System.currentTimeMillis();
        isUpdate = true;
        UpdateRateCountThread updateRateCountThread = new UpdateRateCountThread();
        updateRateCountThread.start();
        //SD60RT 在uhf使用的时候，强制备电给主电充电
        try {
            writer = new BufferedWriter(new FileWriter(file, false));
            writer.write("otgon");
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //取消掩码 Cancel the mask
        iuhfService.selectCard(1, "", false);
        iuhfService.inventoryStart();
        inSearch = true;
        mLlFind.setVisibility(View.GONE);
        mLlPause.setVisibility(View.VISIBLE);
        mTvListMsg.setVisibility(View.VISIBLE);
        mLlListBg.setVisibility(View.GONE);
        mListViewCard.setVisibility(View.VISIBLE);
        mFindBtn.setText(R.string.Stop_Search_Btn);
        mIvMenu.setEnabled(false);
        mBtSearch.setEnabled(false);
        mIvSet.setEnabled(false);
        mEtSearch.setEnabled(false);
        btnExport.setEnabled(false);
        btnExport.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_gray_shape));
        btnExport.setTextColor(getResources().getColor(R.color.text_gray));
        //        updateRateCount();
    }

     private class UpdateRateCountThread extends Thread {
        @Override
        public void run() {
            while (isUpdate) {
                if (UHFUtil.isStopTime) {
                    int stopTime = Integer.parseInt(UHFUtil.mStopTime);
                    if ((System.currentTimeMillis() - startCheckingTime) >= (stopTime * 1000)) {
                        handler.sendEmptyMessage(5);
                    }
                }
                handler.sendEmptyMessage(4);
                SystemClock.sleep(10);
            }
        }
    }
private volatile boolean isUpdate = false;
     /**
     * 停止盘点
     * Stop inventory
     */
    private void stopUhf() {
        if (iuhfService == null) {
            return;
        }
        iuhfService.inventoryStop();
        inSearch = false;
        mFindBtn.setText(R.string.Start_Search_Btn);
        mIvMenu.setEnabled(true);
        mBtSearch.setEnabled(true);
        mIvSet.setEnabled(true);
        mEtSearch.setEnabled(true);
        btnExport.setEnabled(true);
        btnExport.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_b_g_select));
        btnExport.setTextColor(getResources().getColor(R.color.text_white));
        try {
            writer = new BufferedWriter(new FileWriter(file, false));
            writer.write("otgoff");
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        isUpdate = false;
    }
    private SoundPool soundPool;
    private int soundId;
    private long scant = 0;
    private KProgressHUD kProgressHUD;
     @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            boolean cn = "CN".equals(MainActivity.this.getApplicationContext().getResources().getConfiguration().locale.getCountry());
            switch (msg.what) {
                case 1:
                    scant++;
                    long iop = System.currentTimeMillis();
                    if (mTbtnSound.isChecked()) {
                        soundPool.play(soundId, 1, 1, 0, 0, 1);
                    }
                    SpdInventoryData var1 = (SpdInventoryData) msg.obj;
                    int j;
                    for (j = 0; j < uhfCardBeanList.size(); j++) {
                        if (var1.epc.equals(uhfCardBeanList.get(j).getEpc())) {
                            int v = uhfCardBeanList.get(j).getValid() + 1;
                            uhfCardBeanList.get(j).setValid(v);
                            uhfCardBeanList.get(j).setRssi(var1.rssi);
                            if (!TextUtils.isEmpty(var1.tid)) {
                                uhfCardBeanList.get(j).setTidUser(var1.tid);
                            }
                            break;
                        }
                    }
                    if (j == uhfCardBeanList.size()) {
                        uhfCardBeanList.add(new UhfCardBean(var1.epc, 1, var1.rssi, var1.tid));
                    }
                    Log.e("zzc", "=handleMessage=====" + uhfCardBeanList.size() + " | " + (System.currentTimeMillis() - iop));
                    uhfCardAdapter.notifyDataSetChanged();
                    break;

                case 2:
                    kProgressHUD.dismiss();
                    if (cn) {
                        Toast.makeText(MainActivity.this, "导出完成", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MainActivity.this, "Export the complete", Toast.LENGTH_SHORT).show();
                    }
                    break;

                case 3:
                    kProgressHUD.dismiss();
                    if (cn) {
                        Toast.makeText(MainActivity.this, "导出过程中出现问题！请重试", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MainActivity.this, "There is a problem in exporting! Please try again", Toast.LENGTH_SHORT).show();
                    }
                case -1:
                    int status = (int) msg.obj;
                    Toast.makeText(MainActivity.this, ErrorStatus.getErrorStatus( status), Toast.LENGTH_SHORT).show();
                    break;
                case 4:
                    updateRateCount();
                    break;
                case 5:
                    stopUhf();
                    break;
                default:
                    break;
            }

        }
    };

      private long startCheckingTime;
 /**
     * 更新显示数据
     * Update display data
     */
    private void updateRateCount() {
        Log.e("zzc", "==updateRateCount==");
        long mlEndTime = System.currentTimeMillis();

        double rate = Math.ceil((scant * 1.0) * 1000 / (mlEndTime - startCheckingTime));

        long totalTimeCount = mlEndTime - startCheckingTime;

        speedTv.setText(String.format("%s" + getResources().getString(R.string.num), String.valueOf(rate)));

        tagNumTv.setText(String.format("%s", String.valueOf(uhfCardBeanList.size())));

        totalTime.setText(String.format(getResources().getString(R.string.spend_time) + "%s", String.valueOf(getTimeFromMillisecond(totalTimeCount))));
    }

    /**
     * 从时间(毫秒)中提取出时间(时:分:秒)
     * extract time from time (ms) (hours: minutes: seconds)
     * 时间格式:  时:分:秒.毫秒
     * time format: time: minutes: seconds. Milliseconds
     *
     * @param millisecond 毫秒 Milliseconds
     * @return 时间字符串 Time string
     */
    public static String getTimeFromMillisecond(Long millisecond) {
        String milli;
        int num = 100;
        //根据时间差来计算小时数
        //Calculate the hours according to the time difference
        long hours = millisecond / (60 * 60 * 1000);
        //根据时间差来计算分钟数
        //Calculate the minutes according to the time difference
        long minutes = (millisecond - hours * (60 * 60 * 1000)) / (60 * 1000);
        //根据时间差来计算秒数
        //Calculate the seconds according to the time difference
        long second = (millisecond - hours * (60 * 60 * 1000) - minutes * (60 * 1000)) / 1000;
        //根据时间差来计算毫秒数
        //Calculate the milliseconds according to the time difference
        long milliSecond = millisecond - hours * (60 * 60 * 1000) - minutes * (60 * 1000) - second * 1000;
        if (milliSecond < num) {
            milli = "0" + milliSecond;
        } else {
            milli = "" + milliSecond;
        }
        if (hours == 0) {
            if (minutes == 0) {
                return second + "." + milli + "s";
            }
            return minutes + "m: " + second + "." + milli + "s";
        }
        return hours + "h: " + minutes + "m: " + second + "." + milli + "s";
    }
    private class ExportThread extends Thread {
        @Override
        public void run() {
            String time = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA).format(new Date(System.currentTimeMillis()));
            List<EPCBean> epcBeanList = new ArrayList<>();
            for (UhfCardBean uhfCardBean : uhfCardBeanList) {
                EPCBean epcBean = new EPCBean();
                epcBean.setEPC(uhfCardBean.getEpc());
                epcBean.setTID_USER(uhfCardBean.getTidUser());
                epcBeanList.add(epcBean);
            }
            if (epcBeanList.size() > 0) {
                try {
                    ExcelUtils.getInstance()
                            //设置表格名称    Set table name
                            .setSHEET_NAME("UHFMsg")
                            //设置标题字体颜色  Set the title font color
                            .setFONT_COLOR(Colour.BLUE)
                            //设置标题字体大小  Set the font size of the title
                            .setFONT_TIMES(8)
                            //设置标题字体是否斜体    Sets whether the title font is italic or not
                            .setFONT_BOLD(true)
                            //设置标题背景颜色      Set the title background color
                            .setBACKGROND_COLOR(Colour.GRAY_25)
                            //设置excel内容     Set excel content
                            .setContent_list_Strings(epcBeanList)
                            .setWirteExcelPath(Environment.getExternalStorageDirectory() + File.separator + "UHFMsg" + time + ".xls")
                            .createExcel(MainActivity.this);
                    handler.sendMessage(handler.obtainMessage(2));
                } catch (Exception e) {
                    e.printStackTrace();
                    handler.sendMessage(handler.obtainMessage(3));
                }
            } else {
                handler.sendMessage(handler.obtainMessage(3));
            }
        }
    }

}